home *** CD-ROM | disk | FTP | other *** search
Text File | 1994-10-28 | 2.5 KB | 108 lines | [TEXT/KAHL] |
- // Rubber Bandit is a simple
- // srcXor rubber-banding example.
- // - DMH, MacDTS, 3/5/91
- //
- // Modification History:
- //
- // 9/23/94 nick make RubberBandIt return the bounding rect,
- // cleaned up discarded some fns
- //
- // Copyright: © 1991-94 by Apple Computer, Inc., all rights reserved.
-
-
- // function prototypes:
-
- Rect RubberBandIt(Point anchorPt);
- Rect *CheckRect(Rect *theRect);
- void DrawStuff(Rect *rubberRect);
-
-
-
- /* ================================================
- RubberBandIt follows the mouse and rubber-bands
- a design based on its position. It retains
- control until the mouse button is released.
- ================================================ */
-
- Rect RubberBandIt(Point anchorPt)
- {
- Point curMousePt = {0, 0};
- Rect rubberRect;
-
- PenMode(srcXor); /* Set pen mode to exclusive or.*/
- PenPat(&gray);
-
- GetMouse(&curMousePt); /* Get current mouse pos. */
-
- rubberRect.top = anchorPt.v; /* Draw initial rectangle. */
- rubberRect.left = anchorPt.h;
- rubberRect.bottom = curMousePt.v;
- rubberRect.right = curMousePt.h;
- DrawStuff(&rubberRect);
-
- while (Button())
- {
- GetMouse(&curMousePt); /* Get current mouse pos… */
-
- if (curMousePt.h != rubberRect.right || /* If mouse moved… */
- curMousePt.v != rubberRect.bottom)
- {
- DrawStuff(&rubberRect); /* Erase old… */
- rubberRect.bottom = curMousePt.v;
- rubberRect.right = curMousePt.h;
- DrawStuff(&rubberRect); /* and draw new. */
- }
- }
-
- DrawStuff(&rubberRect); // Erase old at end. */
- return rubberRect ;
- }
-
-
- /* ================================================
- DrawStuff draws designs in the rectangle
- passed, using the current pen mode, etc.
- ================================================ */
-
- void DrawStuff(rubberRect)
- Rect *rubberRect;
- {
- Rect curRect;
-
- curRect = *rubberRect;
- FrameRect(CheckRect(&curRect));
- }
-
-
-
-
- /* ================================================
- CheckRect makes sure that the top of the
- rectangle passed is ≤ the bottom and the left is
- ≤ the right. FrameRect needs things this way.
- ================================================ */
-
- Rect *CheckRect(theRect)
- Rect *theRect;
- {
- short temp;
-
- if (theRect->top > theRect->bottom) /* Need to reverse top and bottom? */
- {
- temp = theRect->top;
- theRect->top = theRect->bottom;
- theRect->bottom = temp;
- }
-
- if (theRect->left > theRect->right) /* Need to reverse left and right? */
- {
- temp = theRect->left;
- theRect->left = theRect->right;
- theRect->right = temp;
- }
-
- return theRect; /* This makes nested calls easier. */
- }
-
-
-